package com.tedu._09springaop.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * 给计算器添加日志功能的切面类
 * Aspect注解: 表示当前类是一个切面类;
 */
@Aspect
@Component
public class LogAspect {

    /**
     * @Pointcut注解: 定义切入点表达式的方法;
     * @Pointcut("bean(calculatorImpl)")
     * @Pointcut("within(com.tedu._09springaop.aop.CalculatorImpl)")
     */
    @Pointcut("execution(public int com.tedu._09springaop.aop.CalculatorImpl.*(..))")
    public void pointCut(){}

    /**
    * @Before注解: 前置通知,调用目标方法之前执行;
    * */
    @Before("pointCut()")
    public void BeforeMethod(JoinPoint joinPoint){
        // 1.获取目标方法的方法名: joinPoint.getSignature().getName()
        // 2.获取目标方法的参数: joinPoint.getArgs()
        String name = joinPoint.getSignature().getName();
        String args = Arrays.toString(joinPoint.getArgs());
        System.out.println("[前置通知]" + name + "方法开始,参数为:" + args);
    }
    /**
    * @After注解:
    *           1.后置通知,目标方法彻底结束[包括正常结束和异常结束]后执行;
    *           2.后置通知没有权限获取到目标方法的返回值;
    * 切入点表达式:  execution(权限标识符 返回值类型 包名.类名.方法名(参数类型,参数类型))
    * */
    @After("pointCut()")
    public void AfterMethod(JoinPoint joinPoint){
        String name = joinPoint.getSignature().getName();
        System.out.println("[后置通知]" + name + "方法结束");
    }
    /**
     * @AfterReturning注解:
     *   1.目标方法正常结束后执行,如果是异常结束则不执行;
     *   2.可以获取到目标方法的返回值;
     * 参数说明:
     *   1.value参数: 切入点表达式;
     *   2.returning参数: 用于接收目标方法的返回值;
     */
    @AfterReturning(value = "pointCut()", returning = "result")
    public void AfterReturningMethod(JoinPoint joinPoint, Object result){
        String name = joinPoint.getSignature().getName();
        System.out.println("[返回通知]" + name + "方法正常结束,返回值为:" + result);
    }

    /**
     * @AfterThrowing注解:
     *   1.异常通知,目标方法抛出异常时执行,如果正常结束则不执行;
     *   2.可以获取到目标方法的异常信息;
     */
    @AfterThrowing(value = "pointCut()", throwing = "ex")
    public void AfterThrowingMethod(JoinPoint joinPoint, Throwable ex){
        String name = joinPoint.getSignature().getName();
        System.out.println("[异常通知]" + name + "方法异常结束,异常信息为:" + ex.getMessage());
    }

    /**
     * @Around注解:
     *   1.环绕通知,等价于 @Before + @After + @AfterReturning + @AfterThrowing 注解组合;
     *   2.通常被 try ... catch ... finally ...结构包裹;
     */
    @Around("execution(public Double com.tedu._09springaop.aop.CalculatorImpl.*(..))")
    public Object AroundMethod(ProceedingJoinPoint joinPoint){
        Object result = null;
        /*
            try {
                1.前置通知;
                2.执行目标方法; joinPoint.proceed();
                3.返回通知;
            } catch (){
                4.异常通知;
            } finally {
                5.后置通知;
            }
        */
        try {
            System.out.println("[环绕通知]前置通知");
            result = joinPoint.proceed();
            System.out.println("[环绕通知]返回通知");
        } catch (Throwable e) {
            System.out.println("[环绕通知]异常通知");
            throw new RuntimeException(e);
        } finally {
            System.out.println("[环绕通知]后置通知");
        }
        return result;
    }

}
